<?php

/*
 *	实名认证
 */

class Api_Auth extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			'getAuth' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
			),
			'setAuth' => array(
				'uid'          => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token'        => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'real_name'    => array('name' => 'real_name', 'type' => 'string', 'require' => true, 'desc' => '真实姓名'),
				'mobile'       => array('name' => 'mobile', 'type' => 'string', 'require' => true, 'desc' => '手机号'),
				'cer_no'       => array('name' => 'cer_no', 'type' => 'string', 'require' => true, 'desc' => '身份证号'),
				'front_view'   => array('name' => 'front_view', 'type' => 'string', 'require' => true, 'desc' => '证件正面'),
				'back_view'    => array('name' => 'back_view', 'type' => 'string', 'require' => true, 'desc' => '证件反面'),
				'handset_view' => array('name' => 'handset_view', 'type' => 'string', 'require' => true, 'desc' => '手持证件照'),
			),
		);
	}
	
	
	/**
	 * 获取用户认证信息
	 * @desc 用于获取用户认证信息
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function getAuth()
	{
		$uid        = checkNull($this->uid);
		$token      = checkNull($this->token);
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		} elseif($checkToken == 10020){
			return $this->fail('该账号已被禁用！', 700);
		}
		
		// 用户信息
		$domain = new Domain_Auth();
		$info   = $domain->getAuth($uid);
		
		return $this->success([0 => $info]);
	}
	
	/**
	 * 用户提交认证信息
	 * @desc 用于用户提交认证信息
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function setAuth()
	{
		// 参数
		$uid       = checkNull($this->uid);
		$token     = checkNull($this->token);
		$real_name = checkNull($this->real_name);
		$mobile    = checkNull($this->mobile);
		$cer_no    = checkNull($this->cer_no);
		
		$front_view   = checkNull($this->front_view);
		$back_view    = checkNull($this->back_view);
		$handset_view = checkNull($this->handset_view);
		$checkToken   = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		} elseif($checkToken == 10020){
			return $this->fail('该账号已被禁用！', 700);
		}
		if($real_name == ''){
			return $this->fail('请填写真实姓名！', 1001);
		}
		if(mb_strlen($real_name) > 10){
			return $this->fail('姓名长度在10个字符以内！', 1001);
		}
		if($cer_no == ''){
			return $this->fail('请输入身份证号码！', 1001);
		}
		if(!isCreditNo($cer_no)){
			return $this->fail('身份证号不合法！', 1001);
		}
		if($mobile == ''){
			return $this->fail('请输入正确手机号码！', 1001);
		}
		if($front_view == ''){
			return $this->fail('请上传证件正面！', 1001);
		}
		if($back_view == ''){
			return $this->fail('请上传证件反面！', 1001);
		}
		if($handset_view == ''){
			return $this->fail('请上传手持证件正面照！', 1001);
		}
		
		//$ismobile=checkMobile($mobile);
		$data   = [
			'uid'          => $uid,
			'real_name'    => $real_name,
			'mobile'       => $mobile,
			'cer_no'       => $cer_no,
			'front_view'   => $front_view,
			'back_view'    => $back_view,
			'handset_view' => $handset_view,
			'addtime'      => time(),
			'reason'       => '',
			'status'       => '0',
		];
		$domain = new Domain_Auth();
		$info   = $domain->setAuth($data);
		if(!$info){
			return $this->fail('提交失败,请稍后重试！', 1001);
		}
		unset($data['reason']);
		unset($data['addtime']);
		
		//处理图片路径
		$data['front_view']   = get_upload_path($data['front_view']);
		$data['back_view']    = get_upload_path($data['back_view']);
		$data['handset_view'] = get_upload_path($data['handset_view']);
		
		return $this->success([0 => $data]);
	}
	
}

